Attribute VB_Name = "D3d"
'The 2D (Transformed and Lit) vertex format type.
Public Type TLVERTEX

    x As Single
    y As Single
    z As Single
    RHW As Single
    Color As Long
    Specular As Long
    TU As Single
    TV As Single
    
End Type

'Some color depth constants to help make the DX constants more readable.
Public Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
Public Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
Public Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8

'The 2D (Transformed and Lit) vertex format.
Public Const FVF_TLVERTEX As Long = D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR

Public DirectX8 As DirectX8 'The master DirectX object.
Public Direct3D As Direct3D8 'Controls all things 3D.
Public Direct3D_Device As Direct3DDevice8 'Represents the hardware rendering.

Public Fullscreen_Enabled As Boolean 'Helps determine whether it's fullscreen mode.
Public Running As Boolean 'Helps determine whether the main game loop is running.

Public Vertex_List() As TLVERTEX '4 vertices will make a square.
Public Vertex_Buffer As Direct3DVertexBuffer8
Private Direct3DX As D3DX8

Public Textures() As Direct3DTexture8
Public Texlastused() As Double 'Last time the texture was used

Private Type point
    x As Long
    y As Long
    z As Long
End Type

Public Type Entitiy
    Vlist() As TLVERTEX
    renders() As renderstage
    pos As point
End Type

Public Type renderstage
    startvert As Double
    length As Double
    type As CONST_D3DPRIMITIVETYPE
    tex As Double
End Type


Public Sub Unloadtex()
For x = 0 To UBound(Texlastused)
If Timer - Texlastused(x) > 5 Then
    Set Textures(x) = Nothing
    Texlastused(x) = 0
End If
Next x
End Sub

'This function will make it much easier to setup the vertices with the info it needs.
Public Function Create_TLVertex(x As Single, y As Single, z As Single, RHW As Single, Color As Long, Specular As Long, TU As Single, TV As Single) As TLVERTEX

    Create_TLVertex.x = x
    Create_TLVertex.y = y
    Create_TLVertex.z = z
    Create_TLVertex.RHW = RHW
    Create_TLVertex.Color = Color
    Create_TLVertex.Specular = Specular
    Create_TLVertex.TU = TU
    Create_TLVertex.TV = TV
    
End Function

Public Sub Init(Hwnd As Long)
    Dim Display_Mode As D3DDISPLAYMODE 'Display mode desciption.
    Dim Direct3D_Window As D3DPRESENT_PARAMETERS 'Backbuffer and viewport description.
    
    Set DirectX8 = New DirectX8 'Creates the DirectX object.
    Set Direct3D = DirectX8.Direct3DCreate() 'Creates the Direct3D object using the DirectX object.
    Set Direct3DX = New D3DX8
    
    Display_Mode.Width = 800
    Display_Mode.Height = 600
    Display_Mode.Format = COLOR_DEPTH_16_BIT
    
    Direct3D_Window.Windowed = False 'The app will be in fullscreen mode.
    Direct3D_Window.BackBufferCount = 1 '1 backbuffer only
    Direct3D_Window.BackBufferWidth = Display_Mode.Width 'Match the backbuffer width with the display width
    Direct3D_Window.BackBufferHeight = Display_Mode.Height 'Match the backbuffer height with the display height
    Direct3D_Window.hDeviceWindow = Hwnd 'Use frmMain as the device window.
        
    Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC 'Refresh when the monitor does.
    Direct3D_Window.BackBufferFormat = Display_Mode.Format 'Sets the format that was retrieved into the backbuffer.
    
    Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)
    
    'Include the mag and min filters
    Direct3D_Device.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR
    Direct3D_Device.SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR
    
End Sub

Public Sub Term()
Set Direct3D_Device = Nothing
Set Direct3D = Nothing
Set DirectX8 = Nothing
End Sub


Public Sub Loadtex(ind As Long)

    Dim File_Path As String
    Dim Width As Long
    Dim Height As Long
    Dim Transparency_Color As Long
    
    File_Path = App.Path & "\" & ind
    If Dir(File_Path & ".jpg") <> "" Then
        File_Path = File_Path & ".jpg"
    ElseIf Dir(File_Path & ".gif") <> "" Then
        File_Path = File_Path & ".gif"
    ElseIf Dir(File_Path & ".bmp") <> "" Then
        File_Path = File_Path & ".bmp"
    End If
    
    Width = 256
    Height = 256
    
    Transparency_Color = D3DColorRGBA(255, 255, 255, 255)


    Set Textures(ind) = Direct3DX.CreateTextureFromFileEx(Direct3D_Device, _
                                                    File_Path, _
                                                    Width, Height, _
                                                    0, _
                                                    0, _
                                                    D3DFMT_A8R8G8B8, _
                                                    D3DPOOL_MANAGED, _
                                                    D3DX_FILTER_POINT, _
                                                    D3DX_FILTER_POINT, _
                                                    Transparency_Color, _
                                                    ByVal 0, _
                                                    ByVal 0)
End Sub

